package com.jvm123.minio.config;

import com.jvm123.minio.service.MinioFileService;
import com.jvm123.minio.service.client.MinioClientProvider;
import com.jvm123.minio.service.client.DefaultClientProvider;
import com.jvm123.minio.service.temp.DefaultTempCleanService;
import com.jvm123.minio.service.temp.TempCleanService;

import io.minio.MinioClient;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yawn http://jvm123.com
 * 2020/1/15 15:53
 */
@Configuration
@ConditionalOnClass(MinioClient.class)
@ConditionalOnProperty(prefix=MinioProperties.PREFIX, name="enable", matchIfMissing=true)
@EnableConfigurationProperties(MinioProperties.class)
public class MinioAutoConfiguration {

	private MinioProperties properties;
	
    public MinioAutoConfiguration(MinioProperties properties) {
		this.properties = properties;
	}

    @Bean
    @ConditionalOnMissingBean(TempCleanService.class)
    TempCleanService tempCleanService() {
        return new DefaultTempCleanService();
    }

    @Bean
    @ConditionalOnMissingBean(MinioClientProvider.class)
    MinioClientProvider minClient() {
        return new DefaultClientProvider();
    }

	@Bean
    MinioFileService MinioFileService(TempCleanService tempCleanService, MinioClientProvider clientProvider) {
        return new MinioFileService(properties, tempCleanService, clientProvider);
    }
}
